/**
 * 
 */
package com.base.myproject.client.busi.contract;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.business.BusinessRatify;
import com.base.myproject.client.busi.business.RatifyInfo;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncStartFlow;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 
 * 销售合同
 * busi 1002
 * @author zgw
 *2009-7-2 下午08:52:31
 * javazgw@gmail.com
 */
public class SaleContract  extends BaseJDBCPanel implements BusinessRatify{
	static BaseStore<JDBCModelData> store = null;
	static BaseGrid grid = null;
	//业务代码
	final static String busi = SystemMessage.busi[1];
	Button make = new Button("审批");
	public SaleContract() {
		super();
		init();
	}

	public void init() {

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	this.add(initWeiget(),centerData);
		
		
		registerMainStore(store);
		registerStore(store);
		
		this.setBusino(busi);
		setdefault();
		initbutton();
	}
	
	public void complete()
	{
		
		getToolbarll().add(make);
		super.complete();
	}
	void initbutton()
	{
		make.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				CommandSyncContainer list = new CommandSyncContainer();
				
				CommandSyncStartFlow ratifystart = new CommandSyncStartFlow();
				
				list.add(ratifystart);
				RatifyInfo ratifyinfo = new RatifyInfo();
				ratifyinfo.setBusi(busi);
				ratifyinfo.setIcode(store.getValue("icode").toString());
				ratifyinfo.setFlowname("simple1");
				//ratifyinfo.getVariables().put("content", "to task2");
				ratifystart.setRatifyinfo(ratifyinfo);
				com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list, new AsyncCallback<CommandSyncReturnObject> ()
				{

					@Override
					public void onFailure(Throwable caught) {
						
						MessageBox.alert("错误","网络连接错误，请重新尝试", null);
					}

					@Override
					public void onSuccess(
							CommandSyncReturnObject result) {
						if(result.isB())
						{
							Info.display("", "提交成功");
						}
						else
						{
							MessageBox.alert("错误", result.getMessage(), null);
						}
					}
			
				});
				
			}
			
		});
	}
	private void setdefault()
	{
		store.addDefault("busicode", busi);
	}

	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
		panel.setLayout(new BorderLayout());
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150);
		westData.setSplit(true);
		westData.setCollapsible(true);
		westData.setMargins(new Margins(0));
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
		panel.add(CreateCcodeLeftPanel(), westData);

		TabPanel tabpanel = new TabPanel();
		tabpanel.setWidth(450);
		tabpanel.setHeight(450); 
		tabpanel.setAutoHeight(true);
		TabItem normal = new TabItem("基本信息");
		normal.add(creatContract());
		TabItem gcode = new TabItem("商品信息");
		gcode.add(CreateGCodePanel());
		tabpanel.add(normal);
		tabpanel.add(gcode);
		panel.add(tabpanel, centerData);
		
		
		return panel;
	}
/**
 * 合同管理左边查询
 * @return
 */
	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cname = new ColumnConfig("codename", "合同编号", 200);
		TextField<String> gnametext = new TextField<String>();

		columns.add(cname);
		DateField dateField = new DateField();

		ColumnModel cm = new ColumnModel(columns);

		// --set modetype
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode"); 
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("outordercode");
		type.addField("subscribedate");
		type.addField("odate");
		type.addField("backodate");
		type.addField("ncode");
		type.addField("ccodecommisionh");
		type.addField("signsite");
		type.addField("priceterm");
		type.addField("targport");
		type.addField("targbport");
		type.addField("shipmode");
		type.addField("tarplace");
		type.addField("markdesc");
		type.addField("mark");
		// type.addField("Phone");
		// type.addField("imagepath");
		StoreInfo info = new StoreInfo();
		info.setTablename("contract");
		info.setSql("select * from contract where busicode ='"+busi+"'");
		info.setKeyname(new String[] { "codename" });

		BaseBusiLeftPanel basbusileftpanel = new BaseBusiLeftPanel(columns,
				type, info);
		store = basbusileftpanel.getStore();
		
		basbusileftpanel.getGrid().setAutoExpandColumn("codename");
		setMaincomponent(basbusileftpanel.getGrid());
		return basbusileftpanel.getPanel();
	}
	private ContentPanel creatContract()
	{
		ContentPanel panel = new ContentPanel();
		panel.setAutoHeight(true);
		panel.setAutoWidth(true);
		BaseFormPanel leftpanel = new BaseFormPanel(store);
		
		leftpanel.setLayout(new FormLayout());
		FormData fd = new FormData(200, 30);
		leftpanel.setHeaderVisible(false);
		leftpanel.setBodyBorder(false);
		
		BaseFormPanel rightpanel = new BaseFormPanel(store);
		rightpanel.setBodyBorder(false);
		rightpanel.setHeaderVisible(false);
		
		
		TextField<String> codename = new TextField<String>();
		codename.setName("codename");
		codename.setAllowBlank(false);
		codename.setFieldLabel("合同编码");
		TextField<String> outordercode = new TextField<String>();
		outordercode.setName("outordercode");

		outordercode.setFieldLabel("订单号");
		
		TextField<String> subscribedate = new TextField<String>();
		subscribedate.setName("subscribedate");

		subscribedate.setFieldLabel("签署日期");
		
		TextField<String> ccode = new TextField<String>();
		ccode.setName("ccode");
		ccode.setAllowBlank(false);
		ccode.setFieldLabel("客户");
		
		TextField<String> ncode = new TextField<String>();
		ncode.setName("ncode");

		ncode.setFieldLabel("国别/地区");
		
		TextField<String> signsite = new TextField<String>();
		signsite.setName("signsite");

		signsite.setFieldLabel("签约地");
		
		TextField<String> tarplace = new TextField<String>();
		tarplace.setName("tarplace");

		tarplace.setFieldLabel("目的地");
		TextField<String> markdesc = new TextField<String>();
		markdesc.setName("markdesc");

		markdesc.setFieldLabel("唛头");
		
		//----系统信息
		TextField<String> vprepare = new TextField<String>();
		vprepare.setName("vprepare");

		vprepare.setFieldLabel("制单人");
		TextField<String> predate = new TextField<String>();
		predate.setName("predate");

		predate.setFieldLabel("制单日期");
		
		TextField<String> modifydate = new TextField<String>();
		modifydate.setName("modifydate");

		modifydate.setFieldLabel("最后修改日期");
		
		TextField<String> mark = new TextField<String>();
		mark.setName("mark");

		mark.setFieldLabel("备注");
	
		
		leftpanel.add(codename);
	rightpanel.add(outordercode);
		leftpanel.add(subscribedate);
		rightpanel.add(ccode);
		leftpanel.add(ncode);
		rightpanel.add(signsite);
		leftpanel.add(tarplace);
		rightpanel.add(markdesc);
		leftpanel.add(vprepare);
		rightpanel.add(predate);
		leftpanel.add(modifydate);
		rightpanel.add(mark);

		TableData td = new TableData();
		td.setColspan(1);
		td.setRowspan(1);
		// 对齐方式
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		// rightpanel.add(gudong,td2);
		leftpanel.inited();
		rightpanel.inited();
		FieldSet fieldSet = new FieldSet();
		fieldSet.setHeading("合同基本信息");
		fieldSet.setLayout(new TableLayout(2));
		fieldSet.add(leftpanel,td);
		fieldSet.add(rightpanel,td);
		panel.add(fieldSet);
		return panel;
	}
	/**
	 * 商品
	 * @return
	 */
	private BaseGrid CreateGCodePanel() {

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig cgcode = new ColumnConfig("cgcode", "公司货号", 100);
		TextField<String> cgcodetext = new TextField<String>();
		cgcode.setEditor(new CellEditor(cgcodetext));
		columns.add(cgcode);
		
		ColumnConfig user_cpms = new ColumnConfig("user_cpms", "产品描述", 100);
		TextField<String> user_cpmstext = new TextField<String>();
		user_cpms.setEditor(new CellEditor(user_cpmstext));
		columns.add(user_cpms);
		ColumnConfig user_bzsm = new ColumnConfig("user_bzsm", "包装说明", 100);
		TextField<String> user_bzsmtext = new TextField<String>();
		user_bzsm.setEditor(new CellEditor(user_bzsmtext));
		columns.add(user_bzsm);
		ColumnConfig qty = new ColumnConfig("qty", "数量", 100);
		TextField<String> qtytext = new TextField<String>();
		qty.setEditor(new CellEditor(qtytext));
		columns.add(qty);
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 100);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		columns.add(qtyunit);
		ColumnConfig upric = new ColumnConfig("upric", "单价", 100);
		TextField<String> uprictext = new TextField<String>();
		upric.setEditor(new CellEditor(uprictext));
		columns.add(upric);


		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("idx");
		type.addField("codename");
		type.addField("cgicode");
		type.addField("cgcode");

		type.addField("user_cpms");
		type.addField("user_bzsm");

		type.addField("gcode");
		type.addField("qty");

		type.addField("qtyunit");
		type.addField("upric");


		StoreInfo info = new StoreInfo();

		info.setTablename("contractgcode");
		info.setSql("select * from contractgcode");
		info.setParent(store);// 关联主表
		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		info.setLinkparentcolumns(new String[] { "codename" }); // 关联主表字段
		info.setKeyname(new String[] { "codename", "idx" }); // 主键
		
		BaseStore contractgcode = new BaseStore<JDBCModelData>(type, info);
		contractgcode.setName("contractgcode");
		ColumnModel cm = new ColumnModel(columns);
		BaseGrid contractgcodegrid = new BaseGrid(contractgcode, cm);

		cbs.setSelectionMode(SelectionMode.SINGLE);

		contractgcodegrid.setSelectionModel(cbs);
		contractgcodegrid.setAutoHeight(true);
		contractgcodegrid.setAutoWidth(true);

		registerStore(contractgcode);
		return contractgcodegrid;
	}

	@Override
	public boolean AfterRatify() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean BeforeRatify() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean OnRatify() {
		// TODO Auto-generated method stub
		return false;
	}
}